package com.tomecode.tools.maven.settings.gui.frm.loading;

import java.net.ConnectException;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import com.tomecode.tools.maven.settings.gui.images.ImageFactory;
import com.tomecode.tools.maven.settings.gui.utils.SWTResourceManager;
import com.tomecode.tools.maven.settings.util.URLTester;
import org.eclipse.swt.layout.grouplayout.GroupLayout;

/**
 * Dialog for testing URL
 * 
 * @author Tomas Frastia
 * @see http://www.tomecode.com or
 *      http://code.google.com/p/maven-settings-editor/
 * 
 */
public final class FrmTestUrl extends Dialog {

	private Shell shlTestingUrl;
	private URLTestTask testTask;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public FrmTestUrl(Shell parent) {
		super(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public final String open(URL url) {
		createContents();
		shlTestingUrl.open();
		shlTestingUrl.layout();
		Display display = getParent().getDisplay();

		testTask = new URLTestTask(url);
		display.syncExec(testTask);
		while (!shlTestingUrl.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return testTask.getMessage();
	}

	/**
	 * Create contents of the dialog.
	 */
	private final void createContents() {
		shlTestingUrl = new Shell(getParent(), getStyle());
		shlTestingUrl.setImage(ImageFactory.MAVEN_SETTINGS_EDITOR_16);
		shlTestingUrl.addDisposeListener(new DisposeListener() {
			public final void widgetDisposed(DisposeEvent paramDisposeEvent) {
				testTask.cancel();
			}
		});
		shlTestingUrl.setSize(450, 123);
		shlTestingUrl.setText("Testing URL");

		Composite composite = new Composite(shlTestingUrl, SWT.NONE);

		Label lblTestingUrl = new Label(composite, SWT.NONE);
		lblTestingUrl.setFont(SWTResourceManager.getFont("Lucida Grande", 11, SWT.BOLD | SWT.ITALIC));
		lblTestingUrl.setText("Testing URL ...");

		ProgressBar progressBar = new ProgressBar(composite, SWT.NONE);

		Label label = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		GroupLayout gl_shlTestingUrl = new GroupLayout(shlTestingUrl);
		gl_shlTestingUrl.setHorizontalGroup(gl_shlTestingUrl.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE));
		gl_shlTestingUrl.setVerticalGroup(gl_shlTestingUrl.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE));
		GroupLayout gl_composite = new GroupLayout(composite);
		gl_composite.setHorizontalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(
				gl_composite
						.createSequentialGroup()
						.add(10)
						.add(gl_composite.createParallelGroup(GroupLayout.LEADING).add(lblTestingUrl, GroupLayout.PREFERRED_SIZE, 430, GroupLayout.PREFERRED_SIZE).add(progressBar, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
								.add(label, GroupLayout.PREFERRED_SIZE, 430, GroupLayout.PREFERRED_SIZE)).add(4)));
		gl_composite.setVerticalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(
				gl_composite.createSequentialGroup().add(10).add(lblTestingUrl, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE).add(6).add(progressBar, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE).add(6).add(label)));
		composite.setLayout(gl_composite);
		shlTestingUrl.setLayout(gl_shlTestingUrl);

	}

	/**
	 * URL test task
	 * 
	 * @author Tomas Frastia
	 * @see http://www.tomecode.com or
	 *      http://code.google.com/p/maven-settings-editor/
	 * 
	 */
	private final class URLTestTask implements Runnable {

		private String message;
		private final URLTester urlTester;

		public URLTestTask(URL url) {
			this.urlTester = new URLTester(url);
		}

		public final void cancel() {
			urlTester.cancel();
		}

		@Override
		public final void run() {

			try {
				urlTester.testConnection(URLTester.TIME_OUT_SECONDS);
			} catch (ConnectException e) {
				message = e.getMessage();
			} finally {
				shlTestingUrl.close();
			}
		}

		private final String getMessage() {
			return message;
		}
	}

}
